package com.huabo.system.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.TaskEntityImpl;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.ProcessSettingMySql;
import com.huabo.system.mysql.entity.TblCirculationMySql;
import com.huabo.system.mysql.entity.TblMyTaskMySql;
import com.huabo.system.mysql.entity.TblOrganizationMySql;
import com.huabo.system.mysql.entity.TblStaffMySql;
import com.huabo.system.mysql.mapper.ProcessSettingMySqlMapper;
import com.huabo.system.mysql.mapper.TblOrganizationMySqlMapper;
import com.huabo.system.mysql.mapper.TblStaffMySqlMapper;
import com.huabo.system.oracle.entity.ProcessSetting;
import com.huabo.system.oracle.entity.TblCirculation;
import com.huabo.system.oracle.entity.TblMyTask;
import com.huabo.system.oracle.entity.TblOrganization;
import com.huabo.system.oracle.entity.TblStaff;
import com.huabo.system.oracle.mapper.ProcessSettingMapper;
import com.huabo.system.oracle.mapper.TblOrganizationMapper;
import com.huabo.system.oracle.mapper.TblStaffMapper;
import com.huabo.system.service.ProcessSettingService;
import com.huabo.system.service.TblCirculationService;
import com.huabo.system.service.TblStaffService;
import com.huabo.system.utils.HttpClient;
import com.huabo.system.utils.ProcessVariableEnum;

import cn.hutool.json.JSONString;

@Service
public class ProcessSettingServiceImpl implements ProcessSettingService {

    @Resource
    private ProcessSettingMapper processSettingMapper;
    @Resource
    private ProcessSettingMySqlMapper processSettingMySqlMapper;
    @Resource
    private TblStaffService tblStaffService;
    @Resource
    private TblCirculationService tblCirculationService;
    @Resource
    private TaskService taskService;

    @Resource
    private TblStaffMapper tblStaffMapper;

    @Resource
    private TblStaffMySqlMapper tblStaffMySqlMapper;

    @Resource
    private TblOrganizationMapper tblOrganizationMapper;

    @Resource
    private TblOrganizationMySqlMapper tblOrganizationMySqlMapper;

    @Override
    public Map<String, Object> findByList(Integer pageNumber, Integer pageSize, String token, String staffId) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

                PageInfo<ProcessSetting> pageInfo = new PageInfo<ProcessSetting>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(processSettingMapper.selectListByPageInfo(pageInfo, staff.getLinkDetp().getOrgid(), staff.getCurrentOrg().getOrgid()));
                pageInfo.setTotalRecord(processSettingMapper.selectCountByPageInfo(pageInfo, staff.getLinkDetp().getOrgid(), staff.getCurrentOrg().getOrgid()));
                resultMap.put("data", pageInfo);


            } else {

                PageInfo<ProcessSettingMySql> pageInfo = new PageInfo<ProcessSettingMySql>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(processSettingMySqlMapper.selectListByPageInfo(pageInfo, staff.getLinkDetp().getOrgid(), staff.getCurrentOrg().getOrgid()));
                pageInfo.setTotalRecord(processSettingMySqlMapper.selectCountByPageInfo(pageInfo, staff.getLinkDetp().getOrgid(), staff.getCurrentOrg().getOrgid()));
                resultMap.put("data", pageInfo);
            }
            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public ProcessSetting get(BigDecimal settingid) {
        return processSettingMapper.selectBySettingId(settingid);

    }

    @Override
    public ProcessSettingMySql getMy(BigDecimal settingmyid) {
        return processSettingMySqlMapper.selectBySettingId(settingmyid);
    }

    @Override
    public void update(ProcessSetting processSetting) {

        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            processSettingMapper.updateByProcessSetting(processSetting);
        } else {
            ProcessSettingMySql processSettingMySql = new ProcessSettingMySql();
            processSettingMySqlMapper.updateByProcessSetting(processSettingMySql);
        }


    }


    @Override
    public Map<String, Object> findByLi(Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();

            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

                PageInfo<ProcessSetting> pageInfo = new PageInfo<ProcessSetting>();
                if (pageSize != null) {
                    pageInfo.setPageSize(pageSize);
                }
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(processSettingMapper.selctOrgid(pageInfo, orgid));
                pageInfo.setTotalRecord(processSettingMapper.select(orgid));
                resultMap.put("data", pageInfo);

            } else {

                PageInfo<ProcessSettingMySql> pageInfo = new PageInfo<ProcessSettingMySql>();
                if (pageSize != null) {
                    pageInfo.setPageSize(pageSize);
                }
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(processSettingMySqlMapper.selctOrgid(pageInfo, orgid));
                pageInfo.setTotalRecord(processSettingMySqlMapper.select(orgid));
                resultMap.put("data", pageInfo);

            }

            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;


        //return processSettingMapper.selectOrgid(orgid);
    }

    @Override
    public String deleteProcessInstance(String[] ids) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return String.valueOf(processSettingMapper.deleteById(ids));
        } else {
            return String.valueOf(processSettingMySqlMapper.deleteById(ids));
        }


    }

    @Override
    public void savemerge(ProcessSetting setting) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            processSettingMapper.savemerge(setting);
        } else {
            ProcessSettingMySql settingMySql = new ProcessSettingMySql();
            processSettingMySqlMapper.savemerge(settingMySql);
        }


    }

    @Override
    public void delete(BigDecimal settingId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            processSettingMapper.deleteBySettingId(settingId);
        } else {
            processSettingMySqlMapper.deleteBySettingId(settingId);
        }


    }

    @Override
    public ProcessSetting getByModuel(String definitionName) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            List<ProcessSetting> moduel = this.processSettingMapper.getByModuel(definitionName);
            if (moduel.size() > 0) {
                return moduel.get(0);
            }
        } else {
            List<ProcessSettingMySql> processs = this.processSettingMySqlMapper.getByModuel(definitionName);
            ProcessSetting proces = null;
            if (processs.size() > 0) {
                proces = new ProcessSetting();
                proces.setSettingId(processs.get(0).getSettingId());
                proces.setStatus(processs.get(0).getStatus());
                proces.setModule(processs.get(0).getModule());
                proces.setRemark(processs.get(0).getRemark());
                proces.setOrgid(processs.get(0).getOrgid());
                proces.setCompanyid(processs.get(0).getCompanyid());

            }
            return proces;
        }

        return null;
    }

    @Override
    public List<String> getButtonsForTransition(String tid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return processSettingMapper.getButtonsForTransition(tid);
        } else {
            return processSettingMySqlMapper.getButtonsForTransition(tid);
        }

    }

    @Override
    public PageInfo todoList(TblStaffUtil staff, int start, int pageSize, String orgName) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        TaskQuery query = taskService.createTaskQuery().orderByTaskCreateTime().desc();
        PageInfo bean = new PageInfo();
        bean.setPageSize(pageSize);
        List<String> jobs = new ArrayList<>();


        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            List<TblCirculation> circulations = new ArrayList<>();
            jobs.add(staff.getStaffid().toString());
            if (null != staff.getTrole()) {
                jobs.add(staff.getTrole().getRname().trim());
                jobs.add(orgName.trim());
            }
            query.taskCandidateGroupIn(jobs);
            if (0 == start) {
                start = 1;
            }
            List<Task> tasks = query.list();
            for (Task task : tasks) {
                //ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
                System.out.println(ProcessVariableEnum.model.toString());
                System.out.println(task.getId());
                TblCirculation val = null;
                try {
                	Object variable = taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString());
                	String string = variable.toString();
                	val = JSON.parseObject(string, new TypeReference<TblCirculation>(){});
                } catch (Exception e) {
                	e.printStackTrace();
                }
                if (val != null && val.getCystaffid() != null) {
                    TblStaff user = tblStaffService.findByOragn(val.getCystaffid());
                    //TblOrganization attribute1 = (TblOrganization) request.getSession().getAttribute("hbOrgName");//当前用户的机构
                    if ((staff.getTrole() != null && staff.getTrole().getRname().equals("审计部负责人")) || (staff.getTrole() != null && staff.getTrole().getRname().equals("审计责任人"))) {
                    	
                    	TblOrganization createrUsercompany = tblOrganizationMapper.selectCompanyInfoByDeptId(user.getOrgid());
                        if (createrUsercompany.getOrgid().equals(staff.getLinkOrg().getOrgid())) {
                            val.setTaskid(task.getId().trim());
                            //val.setOrgid(task.getAssignee());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        } else if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getOrgid().toString().equals(staff.getCurrentOrg().getOrgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            //val.setOrgid(task.getAssignee());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        }
                    } else {
                        //if(user!=null && user.getStaffid().toString().equals(staff.getStaffid().toString())) {
                        val.setTaskid(task.getId().trim());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulations.add(val);
                        //}

                    }
                }
            }


            List<TblMyTask> list = null;
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                list = HttpClient.findByTask(staff.getTrole().getRname(), staff.getStaffid().toString(), 1, 15000);
            } else {
                list = HttpClient.findByTask("", staff.getStaffid().toString(), 1, 15000);
            }


            if (list != null && list.size() > 0) {
                List<TblCirculation> cirs = tblCirculationService.findAllStaffid("");
                if (null != cirs && cirs.size() > 0) {
                    for (TblCirculation tblCirculation : cirs) {
                        if (tblCirculation.getDefinitionid() == null || tblCirculation.getDefinitionid().equals("")) {
                            continue;
                        }
                        for (TblMyTask myTask : list) {
                            if (myTask.getTaskId() == null) {
                                continue;
                            }


                            if (tblCirculation != null && tblCirculation.getBusinesskey() != null && myTask.getProcessInstanceId().equals(tblCirculation.getBusinesskey())) {
                                //tblCirculation.setTaskId(myTask.getTaskId());
                                circulations.add(tblCirculation);
        					/* TblStaff user = tblStaffService.findByOragn(tblCirculation.getCyStaffid());
        					if(user!=null && user.getTblOrganization()!=null && user.getTblOrganization().getFatherorgid().toString().equals(staff.getTblOrganization().getFatherorgid().toString())){
        						circulations.add(tblCirculation);
        					}else if(user!=null && user.getTblOrganization()!=null && user.getTblOrganization().getOrgid().toString().equals(staff.getTblOrganization().getOrgid().toString())){
        						circulations.add(tblCirculation);
        					} */

                            }
                        }

                    }
                }
            }


            Set<TblCirculation> set = new HashSet();
            List<TblCirculation> newList = new ArrayList<>();
            for (Iterator<TblCirculation> iter = circulations.iterator(); iter.hasNext(); ) {
                TblCirculation element = iter.next();
                if (set.add(element))
                    newList.add(element);
            }
            circulations.clear();
            circulations.addAll(newList);
            bean.setPageSize(pageSize);
            bean.setTotalRecord(circulations.size());
            List<TblCirculation> listcir = circulations.subList((start - 1) * pageSize, (int) (start * pageSize < circulations.size() ? start * pageSize : circulations.size()));
            bean.setCurrentPage(start);
            bean.setTlist(listcir);
            return bean;


        } else {
            List<TblCirculationMySql> circulationsMySql = new ArrayList<>();
            jobs.add(staff.getStaffid().toString());
            if (null != staff.getTrole()) {
                jobs.add(staff.getTrole().getRname().trim());
                jobs.add(orgName.trim());
            }
            query.taskCandidateGroupIn(jobs);
            if (0 == start) {
                start = 1;
            }
            List<Task> tasks = query.list();

            for (Task task : tasks) {
                //ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
                System.out.println(ProcessVariableEnum.model.toString());
                System.out.println(task.getId());
                TblCirculationMySql val = null;
                try {
                    val = (TblCirculationMySql) taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString());
                } catch (Exception e) {

                }
                if (val != null && val.getCystaffid() != null) {
                    TblStaffMySql user = tblStaffService.findByMySqlOragn(val.getCystaffid());
                    //TblOrganization attribute1 = (TblOrganization) request.getSession().getAttribute("hbOrgName");//当前用户的机构
                    if ((staff.getTrole() != null && staff.getTrole().getRname().equals("审计部负责人")) || (staff.getTrole() != null && staff.getTrole().getRname().equals("审计责任人"))) {
                        if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getFatherorgid().toString().equals(staff.getCurrentOrg().getFatherorgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            //val.setOrgid(task.getAssignee());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySql.add(val);
                        } else if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getOrgid().toString().equals(staff.getCurrentOrg().getOrgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            //val.setOrgid(task.getAssignee());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySql.add(val);
                        }
                    } else {
                        //if(user!=null && user.getStaffid().toString().equals(staff.getStaffid().toString())) {
                        val.setTaskid(task.getId().trim());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulationsMySql.add(val);
                        //}

                    }
                }
            }

            List<TblMyTaskMySql> lists = null;
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                lists = HttpClient.findByMySqlTask(staff.getTrole().getRname(), staff.getStaffid().toString(), 1, 15000);
            } else {
                lists = HttpClient.findByMySqlTask("", staff.getStaffid().toString(), 1, 15000);
            }


//            List<TblMyTaskMySql> list = new ArrayList<TblMyTaskMySql>(0);
//            TblMyTaskMySql taskMySql = null;
//    		for (TblMyTask tblMyTask : lists) {
//    			taskMySql = new TblMyTaskMySql();
//    			taskMySql.setId(tblMyTask.getId());
//    			
//    			taskMySql.setTaskId(tblMyTask.getTaskId());
//    			taskMySql.setProcessDefinitionId(tblMyTask.getProcessDefinitionId());
//    			taskMySql.setProcessInstanceId(tblMyTask.getProcessInstanceId());
//    			taskMySql.setUsrid(tblMyTask.getUsrid());
//    			taskMySql.setFromid(tblMyTask.getFromid());
//    			taskMySql.setApprover(tblMyTask.getApprover());
//    			taskMySql.setExamination(tblMyTask.getExamination());
//    			taskMySql.setProcessName(tblMyTask.getProcessName());
//    			taskMySql.setResult(tblMyTask.getResult());
//    			taskMySql.setApprovalRole(tblMyTask.getApprovalRole());
//    			taskMySql.setCirid(tblMyTask.getCirid());
//    			taskMySql.setHandle(tblMyTask.getHandle());
//    			taskMySql.setAnalid(tblMyTask.getAnalid());
//    			taskMySql.setFxexam(tblMyTask.getFxexam());
//    			taskMySql.setKzjzexam(tblMyTask.getKzjzexam());
//    			taskMySql.setImgBaseStr(tblMyTask.getImgBaseStr());
//    			list.add(taskMySql);
//			}


            if (lists != null && lists.size() > 0) {
                List<TblCirculationMySql> cirss = tblCirculationService.findAllMySqlStaffid("");
                List<TblCirculationMySql> cirs = new ArrayList<TblCirculationMySql>(0);
                TblCirculationMySql tC = null;
                for (TblCirculationMySql tblCirculationMySql : cirss) {
                    tC = new TblCirculationMySql();
                    tC.setCyid(tblCirculationMySql.getCyid());
                    tC.setCyname(tblCirculationMySql.getCyname());
                    tC.setCydate(tblCirculationMySql.getCydate());
                    tC.setCystate(tblCirculationMySql.getCystate());
                    tC.setCytype(tblCirculationMySql.getCytype());
                    tC.setCyurl(tblCirculationMySql.getCyurl());
                    tC.setCystaffid(tblCirculationMySql.getCystaffid());
                    tC.setTaskid(tblCirculationMySql.getTaskid());
                    tC.setBusinesskey(tblCirculationMySql.getBusinesskey());
                    tC.setDefinitionid(tblCirculationMySql.getDefinitionid());
                    tC.setContractstatus(tblCirculationMySql.getContractstatus());
                    tC.setRecordtype(tblCirculationMySql.getRecordtype());


                    //--------------------------------------------------

                    cirs.add(tC);
                }
                if (null != cirs && cirs.size() > 0) {
                    for (TblCirculationMySql tblCirculationMySql : cirs) {
                        if (tblCirculationMySql.getDefinitionid() == null || tblCirculationMySql.getDefinitionid().equals("")) {
                            continue;
                        }
                        for (TblMyTaskMySql myTaskMySql : lists) {
                            if (myTaskMySql.getTaskId() == null) {
                                continue;
                            }
                            if (tblCirculationMySql != null && tblCirculationMySql.getBusinesskey() != null && myTaskMySql.getProcessInstanceId().equals(tblCirculationMySql.getBusinesskey())) {
                                circulationsMySql.add(tblCirculationMySql);
                                if (tblCirculationMySql.getBusinesskey().toString().equals("200455")) {
                                    System.out.println("============================12=31=23=123123=123=12=3=123=12=31=23=123=1=23=123=");
                                }
                            }
                        }

                    }
                }
            }

            Set<TblCirculationMySql> set = new HashSet();
            List<TblCirculationMySql> newListMySqls = new ArrayList<>();
            for (Iterator<TblCirculationMySql> iter = circulationsMySql.iterator(); iter.hasNext(); ) {
                TblCirculationMySql elementmMySql = iter.next();
                if (set.add(elementmMySql))
                    newListMySqls.add(elementmMySql);
            }
            circulationsMySql.clear();
            circulationsMySql.addAll(newListMySqls);
            bean.setPageSize(pageSize);
            bean.setTotalRecord(circulationsMySql.size());
            List<TblCirculationMySql> listcir = circulationsMySql.subList((start - 1) * pageSize, (int) (start * pageSize < circulationsMySql.size() ? start * pageSize : circulationsMySql.size()));
            bean.setCurrentPage(start);
            bean.setTlist(listcir);
            return bean;
        }

    }


    @Override
    public Map<String, Object> toList(String token, Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        TaskQuery query = (TaskQuery) ((TaskQuery) this.taskService.createTaskQuery().orderByTaskCreateTime()).desc();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCurrentPage(pageNumber);
        pageInfo.setPageSize(pageSize);
        List<TblCirculation> circulations = new ArrayList<TblCirculation>();
        List<TblCirculationMySql> circulationsMySql = new ArrayList<TblCirculationMySql>();
        List<String> jobs = new ArrayList<String>();
        jobs.add(staff.getStaffid().toString());
        if (null != staff.getTrole()) {
            jobs.add(staff.getTrole().getRname().trim());
            jobs.add(staff.getCurrentOrg().getOrgname().trim());
        }
        query.taskCandidateGroupIn(jobs);
        if (0 == pageNumber) {
            pageNumber = 1;
        }
        List<Task> tasks = query.list();
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            for (Task task : tasks) {
            	Object variable = taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString());
            	
            	if(null == variable) {
            		break;
            	}
            	
            	String string = variable.toString();
            	TblCirculation val = JSON.parseObject(string, new TypeReference<TblCirculation>(){});
                if (val != null && val.getCystaffid() != null) {
                    TblStaff user = tblStaffService.findByOragn(val.getCystaffid());
                    if ((staff.getTrole() != null && staff.getTrole().getRname().equals("审计部负责人")) || (staff.getTrole() != null && staff.getTrole().getRname().equals("审计责任人"))) {
                    	TblOrganization createrUsercompany = tblOrganizationMapper.selectCompanyInfoByDeptId(user.getOrgid());
                        if (createrUsercompany.getOrgid().equals(staff.getLinkOrg().getOrgid())) {//审计-角色审批，不同公司下角色审批任务，数据隔离
                            val.setTaskid(task.getId().trim());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        } else if (user != null  && user.getOrgid().toString().equals(staff.getCurrentOrg().getOrgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        }
                    } else {
                        val.setTaskid(task.getId().trim());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulations.add(val);
                    }


                }
            }


            List<TblMyTask> list = null;
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                list = HttpClient.findByTask(staff.getTrole().getRname(), staff.getStaffid().toString(), 1, 1000);
            } else {
                list = HttpClient.findByTask("", staff.getStaffid().toString(), 1, 1000);
            }


            if (list != null && list.size() > 0) {
                List<TblCirculation> cirs = tblCirculationService.findAllString("");
                if (null != cirs && cirs.size() > 0) {
                    for (TblCirculation tblCirculation : cirs) {
                        if (tblCirculation.getDefinitionid() == null || tblCirculation.getDefinitionid().equals("")) {
                            continue;
                        }
                        for (TblMyTask myTask : list) {
                            if (myTask.getTaskId() == null) {
                                continue;
                            }


                            if (tblCirculation != null && tblCirculation.getBusinesskey() != null && myTask.getProcessInstanceId().equals(tblCirculation.getBusinesskey())) {
                                //tblCirculation.setTaskId(myTask.getTaskId());
                                circulations.add(tblCirculation);
                            }
                        }
                    }
                }
            }


            Set<TblCirculation> set = new HashSet();
            List<TblCirculation> newList = new ArrayList<>();
            for (Iterator<TblCirculation> iter = circulations.iterator(); iter.hasNext(); ) {
                TblCirculation element = iter.next();
                if (set.add(element))
                    newList.add(element);
            }
            circulations.clear();
            circulations.addAll(newList);
            
			  Collections.sort(circulations, new Comparator<TblCirculation>(){
			  @Override public int compare(TblCirculation o1, TblCirculation o2) { 
				  return o2.getCydate().compareTo(o1.getCydate()); 
				  } 
			  }
			  );
    		 
            
            pageInfo.setPageSize(5);
            pageInfo.setTotalRecord(circulations.size());
            List<TblCirculation> listcir = circulations.subList((pageNumber - 1) * pageSize, (int) (pageNumber * pageSize < circulations.size() ? pageNumber * pageSize : circulations.size()));
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setTlist(listcir);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
            return resultMap;


        } else {


            for (Task task : tasks) {
                System.out.println(ProcessVariableEnum.model.toString());
                System.out.println(task.getId());
                TblCirculationMySql val = (TblCirculationMySql) taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString().trim());
                if (val != null && val.getCystaffid() != null) {
                    TblStaffMySql user = tblStaffMySqlMapper.findByOrag(val.getCystaffid());
                    if ((staff.getTrole() != null && staff.getTrole().getRname().equals("审计部负责人")) || (staff.getTrole() != null && staff.getTrole().getRname().equals("审计责任人"))) {
                        TblOrganizationMySql byoId = this.tblOrganizationMySqlMapper.findByoId(user.getOrgid());
                        if (user != null && byoId != null && byoId.getFatherorgid().toString().equals(staff.getCurrentOrg().getFatherorgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySql.add(val);
                        } else if (user != null && byoId != null && byoId.getOrgid().toString().equals(staff.getCurrentOrg().getOrgid().toString())) {
                            val.setTaskid(task.getId().trim());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySql.add(val);
                        }
                    } else {
                        val.setTaskid(task.getId().trim());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulationsMySql.add(val);
                    }


                }
            }


            List<TblMyTaskMySql> list = null;
            if (staff.getTrole() != null && staff.getTrole().getRname() != null) {
                list = HttpClient.findByMySqlTask(staff.getTrole().getRname(), staff.getStaffid().toString(), 1, 1000);
            } else {
                list = HttpClient.findByMySqlTask("", staff.getStaffid().toString(), 1, 1000);
            }

            List<TblCirculationMySql> circulation = new ArrayList();
            if (list != null && list.size() > 0) {
                List<TblCirculationMySql> cirs = tblCirculationService.findAllMySqlString("");
                if (null != cirs && cirs.size() > 0) {
                    for (TblCirculationMySql tblCirculation : cirs) {
                        if (tblCirculation.getDefinitionid() == null || tblCirculation.getDefinitionid().equals("")) {
                            continue;
                        }
                        for (TblMyTaskMySql myTask : list) {
                            if (myTask.getTaskId() == null) {
                                continue;
                            }


                            if (tblCirculation != null && tblCirculation.getBusinesskey() != null && myTask.getProcessInstanceId().equals(tblCirculation.getBusinesskey())) {
                                //tblCirculation.setTaskId(myTask.getTaskId());
                                circulation.add(tblCirculation);
                            }
                        }
                    }
                }
            }


            Set<TblCirculationMySql> set = new HashSet();
            List<TblCirculationMySql> newList = new ArrayList<>();
            for (Iterator<TblCirculationMySql> iter = circulationsMySql.iterator(); iter.hasNext(); ) {
                TblCirculationMySql element = iter.next();
                if (set.add(element))
                    newList.add(element);
            }
            circulationsMySql.clear();
            circulationsMySql.addAll(newList);
            pageInfo.setPageSize(5);
            pageInfo.setTotalRecord(circulationsMySql.size());
            List<TblCirculationMySql> listcir = circulationsMySql.subList((pageNumber - 1) * pageSize, (int) (pageNumber * pageSize < circulationsMySql.size() ? pageNumber * pageSize : circulationsMySql.size()));
            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setTlist(listcir);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", pageInfo);
            return resultMap;


        }


    }


    @Override
    public PageInfo todoList(PageInfo<ProcessSetting> pageInfo, TblStaffUtil staff, BigDecimal orgid, BigDecimal staffid) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        TaskQuery query = (TaskQuery) ((TaskQuery) this.taskService.createTaskQuery().orderByTaskCreateTime()).desc();
        PageInfo bean = new PageInfo();
        List<TblCirculation> circulations = new ArrayList();
        List<String> jobs = new ArrayList();
        //staff = this.tblStaffService.findById(staffid.toString());
        jobs.add(staff.getStaffid().toString());
        if (null != staff.getTrole()) {
            jobs.add(staff.getUsername());
            jobs.add(staff.getOrgName());
        }

        query.taskCandidateGroupIn(jobs);

        List<Task> tasks = query.list();
        Iterator lis = tasks.iterator();

        while (true) {
            while (true) {
                while (true) {
                    TaskEntityImpl task;
                    TblCirculation val;
                    do {
                        do {
                            if (!lis.hasNext()) {
                                // list = null;
                                List list;
                                if (staff.getTrole() != null && staff.getRealname() != null) {
                                    list = HttpClient.findByTask(staff.getRealname(), staff.getStaffid().toString(), 1, 1000);
                                } else {
                                    list = HttpClient.findByTask("", staff.getStaffid().toString(), 1, 1000);
                                }

                                if (list != null && list.size() > 0) {
                                    List<TblCirculation> cirs = this.tblCirculationService.findAllStaffid("");
                                    if (null != cirs && cirs.size() > 0) {
                                        Iterator var20 = cirs.iterator();

                                        label92:
                                        while (true) {
                                            TblCirculation tblCirculation;
                                            do {
                                                do {
                                                    if (!var20.hasNext()) {
                                                        break label92;
                                                    }

                                                    tblCirculation = (TblCirculation) var20.next();
                                                } while (tblCirculation.getDefinitionid() == null);
                                            } while (tblCirculation.getDefinitionid().equals(""));

                                            Iterator var15 = list.iterator();

                                            while (var15.hasNext()) {
                                                TblMyTask myTask = (TblMyTask) var15.next();
                                                if (myTask.getTaskId() != null && tblCirculation != null && tblCirculation.getBusinesskey() != null && myTask.getProcessInstanceId().equals(tblCirculation.getBusinesskey())) {
                                                    circulations.add(tblCirculation);
                                                }
                                            }
                                        }
                                    }
                                }

                                Set<TblCirculation> set = new HashSet();
                                List<TblCirculation> newList = new ArrayList();
                                Iterator iter = circulations.iterator();

                                while (iter.hasNext()) {
                                    TblCirculation element = (TblCirculation) iter.next();
                                    if (set.add(element)) {
                                        newList.add(element);
                                    }
                                }

                                circulations.clear();
                                circulations.addAll(newList);
                                bean.setPageSize(5);
                                bean.setCurrentPage(circulations.size());

                                List<TblCirculation> listcir = circulations.subList((pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), (pageInfo.getCurrentPage() * pageInfo.getPageSize()) < circulations.size() ? (pageInfo.getCurrentPage() * pageInfo.getPageSize()) : circulations.size());
                                bean.setCurrentPage(pageInfo.getCurrentPage());
                                bean.setTlist(listcir);
                                return bean;
                            }

                            task = (TaskEntityImpl) lis.next();
                            System.out.println(ProcessVariableEnum.model.toString());
                            System.out.println(task.getId());
                            val = (TblCirculation) this.taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString());
                        } while (val == null);
                    } while (val.getCystaffid() == null);

                    TblStaff user = this.tblStaffService.findByOragn(val.getCystaffid());
                    if (staff.getTrole() != null && staff.getRealname().equals("审计部负责人") || staff.getTrole() != null && staff.getRealname().equals("审计责任人")) {
                        if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getFatherorgid().toString().equals(staff.getOrgFatherName())) {
                            val.setTaskid(task.getId().toString());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        } else if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getOrgid().toString().equals(staff.getOrgid())) {
                            val.setTaskid(task.getId().toString());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulations.add(val);
                        }
                    } else {
                        val.setTaskid(task.getId().toString());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulations.add(val);
                    }
                }
            }
        }

    }

    @Override
    public PageInfo todoMySqlList(PageInfo<ProcessSettingMySql> pageInfo, TblStaffUtil staff, BigDecimal
            orgid, BigDecimal staffid) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        TaskQuery query = (TaskQuery) ((TaskQuery) this.taskService.createTaskQuery().orderByTaskCreateTime()).desc();
        PageInfo bean = new PageInfo();
        List<TblCirculationMySql> circulationsMySqls = new ArrayList();
        List<String> jobs = new ArrayList();
        //staff = this.tblStaffService.findById(staffid.toString());
        jobs.add(staff.getStaffid().toString());
        if (null != staff.getTrole()) {
            jobs.add(staff.getUsername());
            jobs.add(staff.getOrgName());
        }

        query.taskCandidateGroupIn(jobs);

        List<Task> tasks = query.list();
        Iterator lis = tasks.iterator();

        while (true) {
            while (true) {
                while (true) {
                    TaskEntityImpl task;
                    TblCirculationMySql val;
                    do {
                        do {
                            if (!lis.hasNext()) {
                                // list = null;
                                List list;
                                if (staff.getTrole() != null && staff.getRealname() != null) {
                                    list = HttpClient.findByTask(staff.getRealname(), staff.getStaffid().toString(), 1, 1000);
                                } else {
                                    list = HttpClient.findByTask("", staff.getStaffid().toString(), 1, 1000);
                                }

                                if (list != null && list.size() > 0) {
                                    List<TblCirculationMySql> cirssa = this.tblCirculationService.findAllMySqlStaffid("");
                                    List<TblCirculationMySql> cirs = new ArrayList<TblCirculationMySql>(0);

                                    TblCirculationMySql tC = null;
                                    for (TblCirculationMySql tblCirculationMySql : cirssa) {
                                        tC = new TblCirculationMySql();


                                        tC = new TblCirculationMySql();
                                        tC.setCyid(tblCirculationMySql.getCyid());
                                        tC.setCyname(tblCirculationMySql.getCyname());
                                        tC.setCydate(tblCirculationMySql.getCydate());
                                        tC.setCystate(tblCirculationMySql.getCystate());
                                        tC.setCytype(tblCirculationMySql.getCytype());
                                        tC.setCyurl(tblCirculationMySql.getCyurl());
                                        tC.setCystaffid(tblCirculationMySql.getCystaffid());
                                        tC.setTaskid(tblCirculationMySql.getTaskid());
                                        tC.setBusinesskey(tblCirculationMySql.getBusinesskey());
                                        tC.setDefinitionid(tblCirculationMySql.getDefinitionid());
                                        tC.setContractstatus(tblCirculationMySql.getContractstatus());
                                        tC.setRecordtype(tblCirculationMySql.getRecordtype());


                                        //--------------------------------------------------

                                        cirs.add(tC);


                                    }

                                    if (null != cirs && cirs.size() > 0) {
                                        Iterator var20 = cirs.iterator();

                                        label92:
                                        while (true) {
                                            TblCirculationMySql tblCirculation;
                                            do {
                                                do {
                                                    if (!var20.hasNext()) {
                                                        break label92;
                                                    }

                                                    tblCirculation = (TblCirculationMySql) var20.next();
                                                } while (tblCirculation.getDefinitionid() == null);
                                            } while (tblCirculation.getDefinitionid().equals(""));

                                            Iterator var15 = list.iterator();

                                            while (var15.hasNext()) {
                                                TblMyTaskMySql myTaskMySql = (TblMyTaskMySql) var15.next();
                                                if (myTaskMySql.getTaskId() != null && tblCirculation != null && tblCirculation.getBusinesskey() != null && myTaskMySql.getProcessInstanceId().equals(tblCirculation.getBusinesskey())) {
                                                    circulationsMySqls.add(tblCirculation);
                                                }
                                            }
                                        }
                                    }
                                }

                                Set<TblCirculationMySql> set = new HashSet();
                                List<TblCirculationMySql> newList = new ArrayList();
                                Iterator iter = circulationsMySqls.iterator();

                                while (iter.hasNext()) {
                                    TblCirculationMySql element = (TblCirculationMySql) iter.next();
                                    if (set.add(element)) {
                                        newList.add(element);
                                    }
                                }

                                circulationsMySqls.clear();
                                circulationsMySqls.addAll(newList);
                                bean.setPageSize(5);
                                bean.setCurrentPage(circulationsMySqls.size());

                                List<TblCirculationMySql> listcir = circulationsMySqls.subList((pageInfo.getCurrentPage() - 1) * pageInfo.getPageSize(), (pageInfo.getCurrentPage() * pageInfo.getPageSize()) < circulationsMySqls.size() ? (pageInfo.getCurrentPage() * pageInfo.getPageSize()) : circulationsMySqls.size());
                                bean.setCurrentPage(pageInfo.getCurrentPage());
                                bean.setTlist(listcir);
                                return bean;
                            }

                            task = (TaskEntityImpl) lis.next();
                            System.out.println(ProcessVariableEnum.model.toString());
                            System.out.println(task.getId());
                            val = (TblCirculationMySql) this.taskService.getVariable(task.getId(), ProcessVariableEnum.model.toString());
                        } while (val == null);
                    } while (val.getCystaffid() == null);

                    TblStaff user = this.tblStaffService.findByOragn(val.getCystaffid());
                    if (staff.getTrole() != null && staff.getRealname().equals("审计部负责人") || staff.getTrole() != null && staff.getRealname().equals("审计责任人")) {
                        if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getFatherorgid().toString().equals(staff.getOrgFatherName())) {
                            val.setTaskid(task.getId().toString());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySqls.add(val);
                        } else if (user != null && user.getTblOrganization() != null && user.getTblOrganization().getOrgid().toString().equals(staff.getOrgid())) {
                            val.setTaskid(task.getId().toString());
                            val.setDefinitionid(task.getProcessDefinitionId());
                            circulationsMySqls.add(val);
                        }
                    } else {
                        val.setTaskid(task.getId().toString());
                        val.setDefinitionid(task.getProcessDefinitionId());
                        circulationsMySqls.add(val);
                    }
                }
            }
        }


    }


}
